﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ample.Model;
using Ample.BLL.User;

public partial class Admin_Boards_ManageBoards : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }
    }

    private void BindData()
    {
        var boardBO = new BoardBO();
        var boards = boardBO.GetBoards();
        gvBoards.DataSource = boards;
        gvBoards.DataBind();
    }

    protected void gvBoards_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int boardId = Convert.ToInt32(gvBoards.DataKeys[e.RowIndex][0]);
        var boardBO = new BoardBO();
        Board board = boardBO.GetBoardById(boardId);
        BoardBO.DeleteBoard(board);

        BindData();
    }

    protected void gvBoards_SelectedIndexChanged(object sender, EventArgs e)
    {
        int boardId = Convert.ToInt32(gvBoards.SelectedValue);
        PopulateBoardTreeView(boardId);
        PopulateWaitingUsers(boardId);
    }

    private void PopulateWaitingUsers(int boardId)
    {
        var boardBO = new BoardBO();
        var boardUsers = boardBO.GetBoardWaitingUsersByBoardId(boardId);
         
        var tempBoardUsers = new ArrayList();
        foreach (var user in boardUsers)
        {
            var parentBoardUser = boardBO.GetParentBoardUserByBoardUserId(user.BoardUserId);
            
            string positionMix = user.PositionCode.Trim();
            string[] positions = positionMix.Split(';');

            var tempUser = new { BoardUserId = user.BoardUserId, DistributorID = user.Order.UserName, ReferID = parentBoardUser.Order.UserName,
                Positions = positions, OrderId = user.OrderId};
            tempBoardUsers.Add(tempUser);
        }

        gvNotYetApprovedUsers.DataSource = tempBoardUsers;
        gvNotYetApprovedUsers.DataBind();
    }

    private void PopulateBoardTreeView(int boardId)
    {
        tvBoardStruct.Nodes.Clear();

        var boardBO = new BoardBO();
        var boardUsers = boardBO.GetTreeBoardUsersByBoardId(boardId);

        TreeBoardUser boardUser = boardUsers.Where(b => b.PositionCode.Trim().Contains(EnumPositionCodeType.Green.ToString())).SingleOrDefault();

        //if(boardUser != null)
        //{

        var greennode = new TreeNode();

        if (boardUser != null)
        {
            greennode = new TreeNode(string.Format("{0}:{1}", boardUser.BoardUserName, boardUser.PositionCode));
            greennode.Value = boardUser.Id;
        }

        greennode.Expanded = true;
        greennode.SelectAction = TreeNodeSelectAction.None;
        greennode.ImageUrl = "~/images/GreenNode.png";
        tvBoardStruct.Nodes.Add(greennode);

        var boardUsrYellow = boardUsers.Where(b => b.PositionCode.Trim().Contains(EnumPositionCodeType.Yellow.ToString())).ToList();

        if (boardUsrYellow.Count > 0)
        {
            foreach (var user in boardUsrYellow)
            {
                var yellownode = new TreeNode(string.Format("{0}:{1}", user.BoardUserName, user.PositionCode));
                yellownode.Expanded = true;
                yellownode.SelectAction = TreeNodeSelectAction.None;
                yellownode.ImageUrl = "~/images/YellowNode.png";
                yellownode.Value = user.Id;
                tvBoardStruct.Nodes[0].ChildNodes.Add(yellownode);
            }
        }
        else
        {
            var yellownode = new TreeNode();
            yellownode.Expanded = true;
            yellownode.SelectAction = TreeNodeSelectAction.None;
            yellownode.ImageUrl = "~/images/YellowNode.png";
            tvBoardStruct.Nodes[0].ChildNodes.Add(yellownode);
        }

        var boardUsrRed = boardUsers.Where(b => b.PositionCode.Trim().Contains(EnumPositionCodeType.Red.ToString() )).ToList();

        foreach (var user in boardUsrRed)
        {
            var rednode = new TreeNode(string.Format("{0}:{1}", user.BoardUserName, user.PositionCode));
            rednode.Expanded = true;
            rednode.SelectAction = TreeNodeSelectAction.None;
            rednode.ImageUrl = "~/images/RedNode.png";
            rednode.Value = user.Id;
            
            foreach(TreeNode node in tvBoardStruct.Nodes[0].ChildNodes)
            {
                if(!string.IsNullOrEmpty(user.ParentId))
                {
                    if(node.Value == user.ParentId)
                    {
                        node.ChildNodes.Add(rednode);
                    }
                }
                else
                {
                    tvBoardStruct.Nodes[0].ChildNodes[0].ChildNodes.Add(rednode);
                }
            }
        }

        //}

    }

    protected void gvNotYetApprovedUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow gvr = gvNotYetApprovedUsers.Rows[e.RowIndex];
        if (gvr.RowType == DataControlRowType.DataRow)
        {
            var boardUserId = gvNotYetApprovedUsers.DataKeys[e.RowIndex];
            var iBoardUserId = (int)boardUserId.Value;

            var bo = new BoardBO();
            var boardUser = bo.GetBoardUserByBoardUserId(iBoardUserId);
            BoardBO.DeleteBoardUser(boardUser);

            PopulateBoardTreeView(boardUser.BoardId);
            PopulateWaitingUsers(boardUser.BoardId);
        }

    }

    protected void gvNotYetApprovedUsers_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        GridViewRow gvr = gvNotYetApprovedUsers.Rows[e.NewSelectedIndex];
        if (gvr.RowType == DataControlRowType.DataRow)
        {
            var boardUserId = gvNotYetApprovedUsers.DataKeys[e.NewSelectedIndex];
            var iBoardUserId = (int)boardUserId.Value ;

            var rbl = (RadioButtonList)gvr.Cells[3].Controls[1];

            var selection = rbl.SelectedValue;
            
            if (iBoardUserId != -1 && !string.IsNullOrEmpty(selection))
            {
                var bo = new BoardBO();
                var boardUser = bo.GetBoardUserByBoardUserId(iBoardUserId);

                boardUser.PositionCode = selection;
                boardUser.Approved = true;
                bo.SaveBoardUser(boardUser);

                PopulateBoardTreeView(boardUser.BoardId);
                PopulateWaitingUsers(boardUser.BoardId);
            }
        }
    }
}
